﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace HW8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Task task1 = new Task(()=> { Search(@"https://www.baidu.com/baidu?wd="+textBox1.Text); });
            //Task task2 = new Task(() => { Search(@"https://www.baidu.com/baidu?wd=" + textBox4.Text); });
            //task1.Start();
            //task2.Start();
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            client.DownloadStringCompleted += DownloadCompleted;
            client.DownloadStringAsync(new Uri(@"https://www.bing.com/search?q=" + textBox1.Text));
            
            WebClient client1 = new WebClient();
            client1.Encoding = Encoding.UTF8;
            client1.DownloadStringCompleted += DownloadCompleted1;
            client1.DownloadStringAsync(new Uri(@"https://www.bing.com/search?q=" + textBox4.Text));
            
        }
        //private void Search1(string url)
        //{
        //    WebClient webClient = new WebClient();
        //    try
        //    {
        //        byte[] resdata = webClient.DownloadData(url);
        //        string res = Encoding.UTF8.GetString(resdata);
        //        //string res = webClient.DownloadString(url);
        //        this.BeginInvoke(new MyDelegate(()=>textBox2.Text = res.Substring(0, 100)))
        //    }
        //    catch(Exception e)
        //    {
        //        textBox2.Text = e.Message;
        //    }
            
        //}

        private void DownloadCompleted(Object sender, DownloadStringCompletedEventArgs e)
        {
            if(!e.Cancelled && e.Error == null)
            {
                textBox2.Text = (String)e.Result.Substring(0,200);
            }
            else
            {
                textBox2.Text = "" + e.Error;
            
            }
        }
        private void DownloadCompleted1(Object sender, DownloadStringCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
            {
                textBox3.Text = (String)e.Result.Substring(0,200);
            }
            else
            {
                textBox3.Text = "" + e.Error;
            }
        }
    }
}
